﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class AllEntitiesShouldHaveJob : IValidator
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var entity in context.Metrics.UserStories.Cast<UIEntityBase>().Concat(context.Metrics.Tasks).Concat(context.Metrics.Bugs))
            {
                var field = entity.CustomFields
                    .Where(x => string.Equals("job", x.Name, StringComparison.OrdinalIgnoreCase) || string.Equals("Job (more)", x.Name, StringComparison.OrdinalIgnoreCase))
                    .FirstOrDefault(x => !string.IsNullOrEmpty(x.Value));
                if (field == null)
                {
                    yield return new ValidationResult(
                        string.Format("{0}: {1} Each entity should have assigned job", entity.EntityType, entity.Id),
                        ValidationResultType.Error)
                    {
                        Entity = entity
                    };
                }
            }
        }
    }
}
